//
//  XMAnimationGuideView.m
//  XMKit
//
//  Created by Ce Liu on 2018/5/29.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMAnimationGuideView.h"

@interface XMAnimationGuideView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIImageView *centerImgView;

@property (nonatomic, strong) UIImageView *leftImgView;

@property (nonatomic, strong) UIImageView *rightImgView;

@property (nonatomic, strong) UIImageView *topImgView;

@property (nonatomic, strong) UIImageView *bottomImgView;

@property (nonatomic, assign) CGFloat startContentOffset;

@end

@implementation XMAnimationGuideView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.scrollView];
        
        self.centerImgView.frame = CGRectMake((self.bounds.size.width - 100) / 2, (self.bounds.size.height - 100) / 2, 100, 100);
        [self addSubview:self.centerImgView];
        
        self.leftImgView.frame = CGRectMake(15, CGRectGetMinY(self.centerImgView.frame) - 100, 120, 200);
        [self addSubview:self.leftImgView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
}

- (void)show {
    UIWindow *window = [self lastWindow];
    [window addSubview:self];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
//    CGFloat offsetX = 0.0;
//    if (scrollView.contentOffset.x > _startContentOffset) {
//        // 左滑
//        offsetX = scrollView.contentOffset.x / kSCREENWIDTH - floor(scrollView.contentOffset.x / kSCREENWIDTH);
//        if (scrollView.contentOffset.x - _startContentOffset == kSCREENWIDTH) {
//            offsetX = 1;
//        }
//
//        self.centerImgView.frame = CGRectMake((self.bounds.size.width - 100 * (1 + offsetX)) / 2, (self.bounds.size.height - 100 * (1 + offsetX)) / 2, 100 * (1 + offsetX), 100 * (1 + offsetX));
//        self.leftImgView.frame = CGRectMake(15, CGRectGetMinY(self.centerImgView.frame) - 100 - CGRectGetMinY(self.leftImgView.frame) * offsetX, 120, 200);
//        self.leftImgView.alpha = offsetX;
//    } else {
        // 右滑
//        offsetX = 1 - (scrollView.contentOffset.x / kSCREENWIDTH - floor(scrollView.contentOffset.x / kSCREENWIDTH));
//        self.centerImgView.frame = CGRectMake((self.bounds.size.width - 100 * (1 + offsetX)) / 2, (self.bounds.size.height - 100 * (1 + offsetX)) / 2, 100 * (1 + offsetX), 100 * (1 + offsetX));
//        self.leftImgView.frame = CGRectMake(15, CGRectGetMinY(self.centerImgView.frame) - 100 - CGRectGetMinY(self.leftImgView.frame) * offsetX, 120, 200);
//    }
    
    CGPoint point =  [scrollView.panGestureRecognizer translationInView:self];
    if (point.x > 0 ) {
        NSLog(@"------往左滚动");
    }else{
        NSLog(@"------往右滚动");
        
    }
}

- (UIImageView *)leftImgView {
    if (!_leftImgView) {
        _leftImgView = [[UIImageView alloc] init];
        _leftImgView.backgroundColor = [UIColor colorWithHexString:@"f7f7f7" alpha:1];
        _leftImgView.contentMode = UIViewContentModeScaleAspectFill;
        _leftImgView.clipsToBounds = YES;
        [_leftImgView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1527566021&di=c963ed165961de069adccc841516df6c&src=http://a1.att.hudong.com/84/20/16300001066597134380205535747.jpg"] placeholderImage:nil];
    }
    return _leftImgView;
}

- (UIImageView *)centerImgView {
    if (!_centerImgView) {
        _centerImgView = [[UIImageView alloc] init];
        _centerImgView.backgroundColor = [UIColor colorWithHexString:@"f7f7f7" alpha:1];
        _centerImgView.contentMode = UIViewContentModeScaleAspectFill;
        _centerImgView.clipsToBounds = YES;
        [_centerImgView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527576071857&di=20102245728d417182e0375e84f0b277&imgtype=0&src=http%3A%2F%2Fa3.topitme.com%2Fa%2Fcf%2Fdd%2F11314604046aaddcfao.jpg"] placeholderImage:nil];
    }
    return _centerImgView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(kSCREENWIDTH * 3, kSCREENHEIGHT);
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}

@end
